import { defineStore } from 'pinia'

export const useTabViewStore = defineStore('tabView', {
  state: () => ({
    visitedViews: []
  }),
  actions: {
    addVisitedView(view) {
      if (this.visitedViews.some(v => v.path === view.path)) return
      this.visitedViews.push(Object.assign({}, view))
    },
    delVisitedView(view) {
      return new Promise(resolve => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1)
            break
          }
        }
        resolve([...this.visitedViews])
      })
    },
    delOtherViews(view) {
      return new Promise(resolve => {
        this.visitedViews = this.visitedViews.filter(v => {
          return v.path === view.path
        })
        resolve([...this.visitedViews])
      })
    },
    delLeftViews(viewIndex) {
      return new Promise(resolve => {
        this.visitedViews = this.visitedViews.filter((item, index) => index >= viewIndex)
        resolve([...this.visitedViews])
      })
    },
    delRightViews(viewIndex) {
      return new Promise(resolve => {
        this.visitedViews = this.visitedViews.filter((item, index) => index <= viewIndex)
        resolve([...this.visitedViews])
      })
    },
    delAllViews() {
      return new Promise(resolve => {
        this.visitedViews = []
        resolve([...this.visitedViews])
      })
    }
  }
})